cmake_minimum_required(VERSION 3.13)

project(bio
    LANGUAGES C CXX
    VERSION 1.0
    DESCRIPTION "my ebin project")

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-std=gnu++2a    has_gnu++2a)
check_cxx_compiler_flag(-fcoroutines-ts has_coro_ts)

if (${has_gnu++2a})
else()
    message(FATAL_ERROR "Compiler does not support GNU++2a standard")
endif()
if (${has_coro_ts})
else()
    message(FATAL_ERROR "Compiler does not support coro TS")
endif()

find_package(Threads)

add_executable(bio bio_ev.cpp main.cpp)

set_property(TARGET bio PROPERTY C_STANDARD 11)
set_property(TARGET bio PROPERTY CXX_STANDARD 20)
target_compile_options(bio
    PUBLIC
    $<$<COMPILE_LANGUAGE:CXX>:-fcoroutines-ts>
    $<$<COMPILE_LANGUAGE:CXX>:-stdlib=libc++>
    PRIVATE
    -fsanitize=address
    # -fsanitize=memory
    -fsanitize=undefined
    -fsanitize=leak
    -pipe -Wall -Wextra)
target_link_options(bio
    PUBLIC
    -fsanitize=address
    # -fsanitize=memory
    -fsanitize=undefined
    -fsanitize=leak
    -pipe)
target_link_libraries(bio PUBLIC
    rt c++ m
    ${CMAKE_THREAD_LIBS_INIT})

include(FetchContent)
FetchContent_Declare(
    libev
    CVS_REPOSITORY ":pserver:anonymous@cvs.schmorp.de/schmorpforge"
    CVS_MODULE "libev")
FetchContent_GetProperties(libev)

if (NOT libev_POPULATED)
    message(STATUS "Downloading libev")
    FetchContent_Populate(libev)
    message(STATUS "Downloading libev - done")
    target_include_directories(bio PRIVATE ${libev_SOURCE_DIR})
endif()

FetchContent_Declare(
    picohttpparser
    GIT_REPOSITORY "https://github.com/h2o/picohttpparser"
    GIT_TAG "master"
)
if (NOT picohttpparser_POPULATED)
    message(STATUS "Downloading picohttpparser")
    FetchContent_Populate(picohttpparser)
    message(STATUS "Downloading picohttpparser - done")
    target_include_directories(bio PRIVATE ${picohttpparser_SOURCE_DIR})
    target_sources(bio PRIVATE ${picohttpparser_SOURCE_DIR}/picohttpparser.c)
endif()

target_include_directories(bio PRIVATE ${CMAKE_SOURCE_DIR})

target_compile_definitions(bio PUBLIC
    EV_STANDALONE=1
    EV_MULTIPLICITY=1
    EV_USE_FLOOR=1
    EV_USE_MONOTONIC=1
    EV_USE_REALTIME=1
    EV_USE_NANOSLEEP=1
    EV_USE_EVENTFD=1
    EV_USE_SELECT=1
    EV_USE_POLL=1
    EV_USE_EPOLL=1
    EV_USE_INOTIFY=1)
